//
//  MineViewController.m
//  ExhibitionHall
//
//  Created by 王洋 on 2019/5/28.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "MineTableViewCell.h"
#import "MineUserInfoView.h"
#import "MineViewController.h"
#import "MineUserInfoModel.h"
@interface MineViewController () <
UITableViewDelegate,
UITableViewDataSource
>

@property (nonatomic, strong) MineUserInfoView *userInfoView;
@property (nonatomic, strong) UITableView      *mineListTableView;
@property (nonatomic, strong) UIButton         *logOutButton;
@property (nonatomic, strong) NSMutableArray   *mineListConfig;

@property (nonatomic, strong) UIViewController *selectVC;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self getUserInfoData];
}

- (void)getUserInfoData {
    
    MineUserInfoModel *model = [[MineUserInfoModel alloc] init];
    model.userName = [TTUserModel sharedObject].userName;
    model.userIconURL = [TTUserModel sharedObject].userIconURL;
    [self.userInfoView fillMineHeaderWithModel:model];
}

- (void)initView {
    self.view.backgroundColor = UIColorHex(0xf1f1f1);
    
    [self.view addSubview:self.userInfoView];
    [self.view addSubview:self.mineListTableView];
    [self.view addSubview:self.logOutButton];
    
    [self.userInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self.view);
        make.width.mas_equalTo(mineInfoMenuViewWidth);
        make.height.mas_equalTo(200);
    }];
    
    [self.mineListTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(self.userInfoView.mas_bottom);
        make.bottom.mas_equalTo(-50);
        make.width.mas_equalTo(mineInfoMenuViewWidth);
    }];
    
    [self.logOutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.userInfoView);
        make.top.mas_equalTo(self.mineListTableView.mas_bottom);
        make.bottom.mas_equalTo(self.view);
    }];
    
    for (int i=0; i<self.mineListConfig.count; i++) {
        UIViewController *vc = [NSClassFromString(self.mineListConfig[i][@"class"]) new];
        [self addChildViewController:vc];
    }
    
    [self changeViewController:0];
}

- (void)changeViewController:(NSInteger)index {
    
    [self.selectVC.view removeFromSuperview];
    
    //获取新的
    UIViewController *newVC = self.childViewControllers[index];
    
    //属性更换成新的视图
    self.selectVC = newVC;
    
    //添加新的view到主视图
    [self.view addSubview:newVC.view];
    
    //设置约束
    [newVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self.view);
        make.left.equalTo(self.mineListTableView.mas_right);
    }];
    
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
    return self.mineListConfig.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineTableViewCell"];

    cell.title.text = self.mineListConfig[indexPath.row][@"title"];
    [cell.icon setImage:[UIImage imageNamed:self.mineListConfig[indexPath.row][@"icon"]]];
    return cell;
}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self changeViewController:indexPath.row];
}




- (void)logOut {
    
    [[TTUserModel sharedObject] initialization]; //userModel清空当前用户的所有信息
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[NSClassFromString(@"LoginViewController") new]];
    [UIApplication sharedApplication].keyWindow.rootViewController = navController;
    navController.navigationBar.hidden = YES;
    
    [WHToast showSuccessWithMessage:@"已退出" duration:TT_GLOBAL_DURATION_TIME finishHandler:^{
        
    }];
    
}

- (UITableView *)mineListTableView {
    
    if (!_mineListTableView) {
        _mineListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        _mineListTableView.delegate = self;
        _mineListTableView.dataSource = self;
        _mineListTableView.scrollEnabled = NO;
        
        _mineListTableView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3];
        [_mineListTableView registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:nil] forCellReuseIdentifier:@"MineTableViewCell"];
        _mineListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _mineListTableView;
}

- (MineUserInfoView *)userInfoView {
    if (!_userInfoView) {
        _userInfoView = [[MineUserInfoView alloc] init];
        _userInfoView.backgroundColor = UIColorHex(0xf1f1f1);
    }
    return _userInfoView;
}

- (UIButton *)logOutButton {
    if (!_logOutButton) {
        _logOutButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_logOutButton setTitle:@"退出账号" forState:UIControlStateNormal];
        [_logOutButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_logOutButton addTarget:self action:@selector(logOut) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logOutButton;
}

- (NSMutableArray *)mineListConfig {
    
    if (!_mineListConfig) {
        _mineListConfig = [[NSMutableArray alloc] init];
        
        _mineListConfig = @[
                            @{@"title":@"清除缓存",@"icon":@"setting",@"class":@"WipCacheViewController"}
                            ].mutableCopy;
        
//        @{@"title":@"修改密码",@"icon":@"setting",@"class":@"ChangePSWViewController"}

    }
    return _mineListConfig;
}

@end
